package com.yyx.qq.jobs;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.yyx.qq.QQMain;


/**
 * 执行器抽象类
 * 
 * @author luma
 */
public abstract class AbstractExecutor implements IExecutor {
	protected Queue<IJob> jobs;
	protected QQMain main;
	protected List<IExecutorListener> listeners;
	protected boolean exitOnEmpty;

	/**
	 * 得到一个Job链的所有job个数
	 * 
	 * @param job
	 *            链头部Job
	 * @return job个数
	 */
	private int getJobCount(IJob job) {
		int i = 0;
		if (job.getSuccessLink() != null)
			i += getJobCount(job.getSuccessLink()) + 1;
		if (job.getFailLink() != null)
			i += getJobCount(job.getFailLink()) + 1;
		return i;
	}

	public AbstractExecutor(QQMain main) {
		this.main = main;
		jobs = new LinkedList<IJob>();
		listeners = new ArrayList<IExecutorListener>();
	}

	public void addExecutorListener(IExecutorListener lis) {
		// TODO Auto-generated method stub
		listeners.add(lis);
	}

	public void addJob(IJob job) {
		// TODO Auto-generated method stub
		jobs.offer(job);
	}

	/**
	 * @return 所有Job的个数
	 */
	public int getAllJobCount() {
		// TODO Auto-generated method stub
		int total = 0;
		for (IJob job : jobs)
			total += getJobCount(job) + 1;
		return total;
	}

	public boolean isBlocked() {
		// TODO Auto-generated method stub
		return false;
	}

	public void setExitOnEmpty(boolean exitOnEmpty) {
		// TODO Auto-generated method stub
		this.exitOnEmpty = exitOnEmpty;
	}

}
